package com.feijiang.controller;


import com.feijiang.entity.User;
import com.feijiang.service.IUserService;
import com.feijiang.util.ResponseData;
import com.feijiang.vo.UserVo;
import javax.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-12-28
 */
@RestController
@RequestMapping("/user")
public class UserController {
  @Resource
  IUserService iUserService;

  /**
   * 登录
   * @param user
   * @return
   */
  @RequestMapping("/login")
  public ResponseData login(@RequestBody User user) {
    if (user == null || user.getUsername() == null || user.getPassword() == null) {
      return ResponseData.error("表单不完整");
    }
    User user1 = iUserService.hasUser(user);
    if(user1 == null) {
      return ResponseData.error("账号或密码错误！");
    }
    return ResponseData.success(user1);
  }

  @RequestMapping("/register")
  public ResponseData register(@RequestBody User user) {
    if (user == null || user.getUsername() == null || user.getPassword() == null) {
      return ResponseData.error("表单不完整");
    }
    Boolean aBoolean = iUserService.register(user);
    if (!aBoolean) {
      return ResponseData.error("账号已存在");
    }
    return ResponseData.success("注册成功");
  }

  @RequestMapping("/editPassword")
  public ResponseData editPassword(@RequestBody UserVo userVo) {
    if (userVo == null || userVo.getUsername() == null || userVo.getPassword() == null || userVo.getNewPassword() == null) {
      return ResponseData.error("表单不完整");
    }
    Boolean aBoolean = iUserService.editPassword(userVo);
    if(!aBoolean) {
      return ResponseData.error("当前输入密码错误！");
    }
    return ResponseData.success("修改成功");
  }
}
